<?php
include './include/common.inc.php';
include 'form.class.php';
include './include/workflow.class.php';
$w = new workflow;

$action = isset($action) ? $action : 'manage';
$forward = isset($forward) ? $forward : (HTTP_REFERER ? HTTP_REFERER : $self_url);

switch($action)
{
    case 'status_list' : //内容状态列表

        if(!check_perms('content_status'))
        {
            showmessage("您没有权限进行此操作！");
        }
        $data = $w->status_list();
        include template('admin', 'workflow_status_manage');
        break;

    case 'status_add' : //添加内容状态

        if(!check_perms('content_status'))
        {
            showmessage("您没有权限进行此操作！");
        }
        if(isset($submitform))
        {
            if(!$w->status_add($data))
            {
                $response->alert($w->error?$w->error:'添加失败');
                $response->real_execute();
            }
            else
            {
                $response->alert("添加成功！");
                $response->redirect($forward);
            }
            $response->execute();
        }
        else
        {
            $unused_status = $w->status_unused();
            include template('admin', 'workflow_status_add');
        }
        break;

    case 'status_delete' : //删除内容状态

        if(!check_perms('content_status'))
        {
            showmessage("您没有权限进行此操作！");
        }
        if(!isset($value))
        {
            $response->alert("没有删除任何数据");
            $response->real_execute();
        }
        if($w->status_delete($value))
        {
            $response->alert("删除成功");
            $response->script('self.location=self.location');
        }
        $response->execute();
        break;

    case 'status_edit' : //修改内容状态

        if(!check_perms('content_status'))
        {
            showmessage("您没有权限进行此操作！");
        }
        $value = isset($value) ? intval($value) : 0;
        if(!isset($value))
        {
            showmessage('该数据不存在');
        }
        if(isset($submitform))
        {
            if(!$w->status_edit($value, $data))
            {
                $response->alert($w->error);
                $response->real_execute();
            }
            else
            {
                $response->alert("修改成功！");
            }
            $response->redirect($forward);
            $response->execute();
        }
        else
        {
            $data = $w->status_get($value);
            include template('admin','workflow_status_add');
        }
        break;

    case 'workflow_list' : //审核方案列表

        if(!check_perms('workflow'))
        {
            showmessage("您没有权限进行此操作！");
        }
        $data = $w->workflow_list();
        include template('admin', 'workflow_manage');
        break;

    case 'workflow_add' : //添加审核方案

        if(!check_perms('workflow'))
        {
            showmessage("您没有权限进行此操作！");
        }
        if(isset($submitform))
        {
            if(!$w->workflow_add($data))
            {
//                $response->alert('添加失败');
                $response->real_execute();
            }
            else
            {
                $response->alert("添加成功！");
                $response->redirect($forward);
            }
            $response->execute();
        }
        else
        {
            include template('admin', 'workflow_add');
        }
        break;

    case 'workflow_delete' : //删除审核方案

        if(!check_perms('workflow'))
        {
            showmessage("您没有权限进行此操作！");
        }
        if(!isset($id))
        {
            $response->alert("没有删除任何数据");
            $response->real_execute();
        }
        if($w->workflow_delete($id))
        {
            $response->alert("删除成功");
            $response->script('self.location=self.location');
        }
        $response->execute();
        break;

    case 'workflow_edit' : //修改审核方案

        if(!check_perms('workflow'))
        {
            showmessage("您没有权限进行此操作！");
        }
        $id = isset($id) ? $id : 0;
        if(empty($id))
        {
            showmessage('该数据不存在');
        }
        if(isset($submitform))
        {
            if(!$w->workflow_edit($id, $data))
            {
//                $response->alert("修改失败");
                $response->real_execute();
            }
            else
            {
                $response->alert("修改成功！");
            }
            $response->redirect($forward);
            $response->execute();
        }
        else
        {
            $data = $w->workflow_get($id);
            include template('admin','workflow_add');
        }
        break;

    case 'process_list' : //审核流程列表

        if(!check_perms('workflow'))
        {
            showmessage("您没有权限进行此操作！");
        }
        if(!isset($workflowid))
        {
            showmessage('未知的工作流方案');
        }
        $data = $w->process_list($workflowid);
        include template('admin', 'workflow_process_manage');
        break;

    case 'process_add' : //添加审核流程

        if(!check_perms('workflow'))
        {
            showmessage("您没有权限进行此操作！");
        }
        if(isset($submitform))
        {
            $data['workflowid'] = isset($workflowid) ? $workflowid : 0;
            if(is_array($prestatus))
            {
                $data['prestatus'] = implode(',', $prestatus);
            }
            if(!$w->process_add($data))
            {
                $response->alert($w->error?$w->error:'添加失败');
            }
            else
            {
                $response->alert('添加成功');
                $response->redirect($forward);
            }
            $response->execute();
        }
        else
        {
            $status = cache_read('content_status');
            include template('admin', 'workflow_process_add');
        }
        break;

    case 'process_edit' : //修改审核流程

        if(!check_perms('workflow'))
        {
            showmessage("您没有权限进行此操作！");
        }
        if(!isset($id))
        {
            showmessage("该数据不存在");
        }
        if(isset($submitform))
        {
            if(is_array($prestatus))
            {
                $data['prestatus'] = implode(',', $prestatus);
            }
            if(!$w->process_edit($id, $data))
            {
                $response->alert($w->error?$w->error:'修改失败');
            }
            else
            {
                $response->alert('修改成功');
                $response->redirect($forward);
            }
            $response->execute();
        }
        else
        {
            $status = cache_read('content_status');
            $data = $w->process_get($id);
            include template('admin', 'workflow_process_add');
        }
        break;

    case 'process_delete' : //删除审核流程

        if(!isset($id))
        {
            $response->alert('该数据不存在');
            $response->real_execute();
        }
        if(!check_perms('workflow'))
        {
            showmessage("您没有权限进行此操作！");
        }
        if(!$w->process_delete($id))
        {
            $response->alert('删除失败');
        }
        else
        {
            $response->alert('删除成功');
            $response->script('self.location=self.location');
        }
        $response->execute();
        break;
}
?>
